<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Add_truck
 *
 * @author admin
 */
class add_purchase extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('stock/stock_model');
        $this->load->model('purchase_model');
        $this->load->model('catalog');
    }

    public function index() {
        $this->_set_form();
        $this->load->view('add_purchase', $this->data);
    }

    private function _set_form() {
        $utility = new Config_utility();

        $day = date('d');
        $month = date('m');
        $year = date('Y') + 543;

        $this->data['txt_locate_produce'] = form_textarea(array('name' => 'txtLocateProduce', 'rows' => '3'), set_value('txtLocateProduce'), 'class="form-control" style="resize: none;"');
        $this->data['cmb_day'] = form_dropdown('cmbDay', $utility->list_day(), $day, 'class="form-control"');
        $this->data['cmb_month'] = form_dropdown('cmbMonth', $utility->list_month(), $month, 'class="form-control"');
        $this->data['cmb_year'] = form_dropdown('cmbYear', $utility->list_year(), $year, 'class="form-control"');

        $this->data['list_main_type'] = $this->catalog->list_stock_main_type();
        $this->data['list_sub_type'] = $this->catalog->list_stock_sub_type();
        $this->data['list_brand'] = $this->catalog->list_stock_brand();
        $this->_set_error();
    }

    private function _set_error() {
        $this->data['err_day'] = form_error('cmbDay', '<div class="error">', '</div>');
        $this->data['err_month'] = form_error('cmbMonth', '<div class="error">', '</div>');
        $this->data['err_year'] = form_error('cmbYear', '<div class="error">', '</div>');
    }

    public function validate() {
        $utility = new Config_form();
        $this->form_validation->set_rules('cmbDay', 'Day', 'required');
        $this->form_validation->set_rules('cmbMonth', 'Month', 'required');
        $this->form_validation->set_rules('cmbYear', 'Year', 'required');

        $arrConfirm = $this->input->post('hidArrayConfirm');

        $day = $this->input->post('cmbDay');
        $month = $this->input->post('cmbMonth');
        $year = $this->input->post('cmbYear');
        $date_create = "";
        if ($day != "" && $month != "" && $year != "") {
            $date_create = $year . "/" . $month . "/" . $day;
        }

        $arrExplode = explode(",", $arrConfirm);

        if ($this->form_validation->run() == FALSE) {
            $this->_set_form();
            $this->load->view('add_purchase', $this->data);
        } else {
            if ($arrConfirm != "") {
                $this->purchase_model->save_data($date_create);
                $last_data = $this->purchase_model->get_last_data_repair();

                if ($arrExplode[0] !== '') {
                    foreach ($arrExplode as $index) {
                        $locate_purchase = $this->input->post('txtLocateProcure' . $index);
                        $main_type_id = $this->input->post('cmbMainType' . $index);
                        $sub_type_id = $this->input->post('cmbSubType' . $index);
                        $brand = $this->input->post('cmbBrand' . $index);
                        $autopart_name = $this->input->post('txtAutoPart' . $index);

                        $dataStock = $this->stock_model->get_stock_by_name($main_type_id, $sub_type_id, $brand, $autopart_name);
                        $qty = $this->input->post('txtQty' . $index);
                        $price = $this->input->post('txtPrice' . $index);
                        $this->purchase_model->save_order_data($last_data[0]->id, $locate_purchase, $main_type_id, $sub_type_id, $brand, $dataStock[0]->id, $qty, $price);
                        $this->purchase_model->update_stock($dataStock[0]->id, ($dataStock[0]->stock_qty + $qty), $price);
                    }
                    echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'purchase/home');
                    exit();
                }
            } else {
                $this->_set_form();
                $this->load->view('add_purchase', $this->data);
            }
        }
    }

}
